#include <bits/stdc++.h>
using namespace std;
int dp[101][51][2];
//dp表示当前一共又i个操作，进行了j次修改，方向为k时候的最远距离
int max(int a, int b) {
    return a > b ? a : b;
}

int main() {
    char order[105];
    int n;
    cin>>order>>n;
    int len = strlen(order);
    
    for (int i = len - 1; i >= 0; i--)
        order[i + 1] = order[i];
    order[0] = ' ';
    
    for (int i = 0; i <= len; i++)
        for (int j = 0; j <= n; j++)
            dp[i][j][0] = dp[i][j][1] = -1000000000;
    
    dp[0][0][0] = 0;
    dp[0][0][1] = 0;
    
    for (int i = 1; i <= len; i++)
        for (int j = 0; j <= n; j++)
            for (int k = 0; k <= j; k++)
                if (order[i] == 'F') {
                    if (k % 2 == 1) {
                        dp[i][j][0] = max(dp[i][j][0], dp[i - 1][j - k][0]);
                        dp[i][j][1] = max(dp[i][j][1], dp[i - 1][j - k][0]);
                    } else {
                        dp[i][j][0] = max(dp[i][j][0], dp[i - 1][j - k][0] + 1);
                        dp[i][j][1] = max(dp[i][j][1], dp[i - 1][j - k][1] - 1);
                    }
                } else {
                    if (k % 2 == 1) {
                        dp[i][j][0] = max(dp[i][j][0], dp[i - 1][j - k][0] + 1);
                        dp[i][j][1] = max(dp[i][j][1], dp[i - 1][j - k][1] - 1);
                    } else {
                        dp[i][j][0] = max(dp[i][j][0], dp[i - 1][j - k][1]);
                        dp[i][j][1] = max(dp[i][j][1], dp[i - 1][j - k][0]);
                    }
                }
    
    cout<<max(dp[len][n][0], dp[len][n][1])<<endl;
    
    return 0;
}


